//
//  ZDQuestionViewController.m
//  ZHIDOU
//
//  Created by 向礼 on 2018/12/22.
//  Copyright © 2018 向礼. All rights reserved.
//

#import "ZDQuestionViewController.h"
#import "Find_QuestionNewCollectionViewCell.h"
#import "ZDQuestionFootView.h"
#import "EndingAnswerAlterView.h"
#import "EndQuestionViewController.h"

@interface ZDQuestionViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property(nonatomic ,strong) UICollectionView * collectionView;

@property(nonatomic ,strong)NSMutableArray *dataSource;

@property(nonatomic,strong) UICollectionViewFlowLayout * flowLayout;

@property(nonatomic ,strong)ZDQuestionFootView *footView;

@property(nonatomic ,assign)NSInteger totalNum;

@property(nonatomic ,assign)NSInteger readNum;

@property(nonatomic ,assign)NSInteger currentCell;

@end

@implementation ZDQuestionViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"有理数第一讲";
    
    _dataSource = @[].mutableCopy;
    
    for (int i=0; i< 50; i++) {
        NSDictionary * dic = @{@"qid":[NSString stringWithFormat:@"%i",i],
                               @"qcontent":[NSString stringWithFormat:@"这只是测试的题目这只是测试的题目这只是测试的题目这只是测试的题目%idasdasdasdasdasdasdasdasdasdasdasdasdasdasdasdadasd",i],
                               @"qchoose":@[@{@"aid":@"A",@"acontent":@"这只是测试答案A"},
                                            @{@"aid":@"B",@"acontent":@"这只是测试答案B"},
                                            @{@"aid":@"C",@"acontent":@"这只是测试答案C"},
                                            @{@"aid":@"D",@"acontent":@"这只是测试答案D这只是测试答案D这只是测试答案D这只是测试答案D这只是测试答案D这只是测试答案D"}],
                               @"ranswer":@"B",
                               @"chooseanser":@"0",
                               @"lasttime":@"65",
                               @"isShouldAnswer":i == 0 ? @"1" : @"0"};
        
        [_dataSource addObject:dic];
        
    }
    [self loadBodyView];
    // Do any additional setup after loading the view.
}


-(void)loadBodyView{
    
    _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    _flowLayout.itemSize = CGSizeMake(ZD_screenWidth, ZD_screenHeight - self.bottomSafeAreaInsets -110);
    _flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumLineSpacing = 0;
    self.flowLayout.minimumInteritemSpacing = 0;
   // self.flowLayout.sectionInset = UIEdgeInsetsMake(30, 0, 0, 0);
    
    _collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ZD_screenWidth, ZD_screenHeight - self.bottomSafeAreaInsets -80 ) collectionViewLayout:_flowLayout];
    _collectionView.showsVerticalScrollIndicator = NO;
    _collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView.bounces = NO;
    
    _collectionView.backgroundColor = UIColorFromRGB(0xf5f5f5);
    _collectionView.pagingEnabled = YES;
    
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    
    [_collectionView registerClass:[Find_QuestionNewCollectionViewCell class] forCellWithReuseIdentifier:@"Find_QuestionNewCollectionViewCell"];
    [self.view addSubview:_collectionView];
    [self.view addSubview:self.footView];
}

#pragma mark - lazy
-(ZDQuestionFootView *)footView{
    if (_footView == nil) {
        _footView = [[ZDQuestionFootView alloc]initWithFrame:CGRectMake(0, ZD_screenHeight - self.bottomSafeAreaInsets - 80, ZD_screenWidth, 80)];
        ZD_weakSelf;
        _footView.hander = ^{
            [weakSelf endQuestion];
          //  [EndingAnswerAlterView showAlter];
        };
    }
    return _footView;
}

#pragma mark - delegate && data source

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _dataSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    Find_QuestionNewCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"Find_QuestionNewCollectionViewCell" forIndexPath:indexPath];
    
    if (!cell) {
        cell = [[Find_QuestionNewCollectionViewCell alloc]init];
    }
    
    if (indexPath.row%2 == 0) {
        cell.type = adTypeLabel;
    }
    else
        cell.type = adTypeImg;
    
    cell.dataSource = _dataSource[indexPath.item];
    ZD_weakSelf;
    cell.clickAnswerBlcok = ^(NSDictionary *dic) {
        
//        [weakSelf removeTimer];
        [weakSelf dealWithLocalData:dic index:indexPath.item];
//
    };
    
    return cell;
}

#pragma mark - custom method

-(void)dealWithLocalData:(NSDictionary *)dic index:(NSInteger)index{
    
    [self.dataSource replaceObjectAtIndex:index withObject:dic];
    
    if (index + 1 == _totalNum) {
       // ZXLog(@"题目已经打完了");
    }
    else{
        NSMutableDictionary * mdic = [NSMutableDictionary dictionaryWithDictionary:_dataSource[index + 1]];
        [mdic setValue:@"1" forKey:@"isShouldAnswer"];
        [self.dataSource replaceObjectAtIndex:index + 1 withObject:mdic];
    }
    
    [[NSUserDefaults standardUserDefaults] setObject:_dataSource forKey:@"question"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    
    [self.collectionView reloadItemsAtIndexPaths:@[[NSIndexPath indexPathForItem:index inSection:0]]];
    
    self.readNum ++;
    
    if ([dic[@"chooseanser"] isEqualToString:dic[@"ranswer"]]) {
//        NSLog(@"恭喜您 答对了 💐");
//        self.rightNum ++;
//        [self performSelector:@selector(pushNext) withObject:nil afterDelay:1.0f];
    }
    else{
        NSLog(@"很遗憾 打错了 😢");
//        self.wrongNum ++;
    }
}

#pragma mark - 完成答题
- (void)endQuestion{
    
    //self.currentCell = self.currentCell + 1;
    [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForRow:2 inSection:0] atScrollPosition:UICollectionViewScrollPositionCenteredHorizontally animated:YES];
//    EndQuestionViewController *end=[[EndQuestionViewController alloc]init];
//    [self.navigationController pushViewController: end animated:YES];
}
@end
